set more off
capture log close

//  program:    AnanatEtAl_minmax_figure
//  task:		Generates Figure S1 showing minimum and maximum job losses by state as a percentage of the working age population

//Change working directory to point to the file with the data
cd *** Fill in path name here ***

//Create the log file
log using AnanatEtAl_minmax_figure, text replace

//Load the data
use AnanatEtAl_ACS_data, clear

sort stateno year

//For each state find the min and max values for separations
by stateno: egen minsep = min(seppctyrlag)
by stateno: egen maxsep = max(seppctyrlag)
 
//Find the years in which the minimum and maximum values occured 
gen yearmax = year if seppctyrlag==maxsep
gen yearmin = year if seppctyrlag==minsep
replace yearmax = 0 if yearmax==.
replace yearmin = 0 if yearmin==.
sort stateno year
by stateno: egen yearofmax = max(yearmax)
by stateno: egen yearofmin = max(yearmin)
drop yearmax yearmin

//Collapse to one observation per state
collapse minsep maxsep yearofmax yearofmin (first) state, by(stateno)

//Sort states by maximum separation value
sort maxsep

//Generate a rank order based on maximum separation value
gen maxseprank = _n

//Generate labels for illustrative observations
lab def rank 4 "GA,1997 ", add
lab def rank 10 "AZ, 2003", add
lab def rank 16 "MT, 2004", add
lab def rank 18 "MS, 1996", add
lab def rank 39 "VT, 2012", add
lab def rank 26 "CO, 2002", add
lab def rank 33 "MO, 2009", add
lab def rank 42 "ME, 1996", add
lab def rank 48 "IL, 2002", add
lab def rank 49 "LA, 2006", add
lab def rank 50 "AK, 2010", add

//Leave the labels blank for all other observations
foreach n in 1 2 3 5 6 7 8 9 11 12 13 14 15 17 19 20 21 22 23 24 25 27 28 29 30 31 32 34 35 36 37 38 40 41 43 44 45 46 47 51 {
lab def rank `n' "", add
}

label values maxseprank rank

//Generate graph
twoway (bar maxsep maxseprank, sort) (bar minsep maxseprank, sort), ytitle(% of Working Age Population) xtitle("") xlabel(4 10 16 18 26 33 39 42 48 50, labels labsize(small) angle(stdarrow) valuelabel) title("Minimum and Maximum Job Loss in Each State, 1995 - 2012") note("Source: Bureau of Labor Statistics - http://www.bls.gov/mls/") legend(order(1 "Maximum" 2 "Minimum"))

graph save Graph minmax_figure, replace

clear
log close


